﻿@{
    Layout = "~/Views/Shared/OA/_Tree.cshtml";

    var isAdmin = ManagerAuthorization.Current.User.IsAdmin;
}
@section Toolbar
{
    @Html.MyNet().Button(new ButtonOptions { Text = "新建", Click = "create" })
    @Html.MyNet().Button(new ButtonOptions { Text = "修改", Click = "modify" })
    @if (isAdmin)
    {
        @Html.MyNet().Button(new ButtonOptions { Text = "复制", Power = "CMS_Catalog_Modify", Click = "copy" })
        @Html.MyNet().Button(new ButtonOptions { Text = "配置", Power = "CMS_Catalog_Config" })
        @Html.MyNet().Button(new ButtonOptions { Text = "应用配置到", Click = "applyConfig" })
    }

    @Html.MyNet().Button(new ButtonOptions { Text = "删除", Click = "removee" })
}
@section Tree
{
    <div id="tree" class="tree">
    </div>
}
@section Main
{
    <div id="detail">
    </div>

    @Html.Hidden("IsAdmin", isAdmin)
}
<script type="text/javascript">
    var url = "/CMS/Catalog/";
    var isAdmin = $('#IsAdmin').val() == 'True' || $('#IsAdmin').val() == 'true';
    var keys = ['kcpx', 'zpzs', 'xwzx'];

    jQuery(function () {
        $("#tree").kendoTreeView(
            {
                dataSource:
                {
                    transport: { read: url + "Read" },
                    schema:
                    {
                        data: "Data",
                        model: { children: "childs" },
                        errors: "Errors"
                    },
                    error: function (e) {
                        alert(e.errors);
                    }
                },
                dragAndDrop: true,
                dataBound: onDataBound,
                select: onSelect,
                dragend: onDragend
            });
    });

    function create() {
        var selectItem = treeSelectedItem();
        var isOk = false;

        for (var i = 0; i < keys.length; i++) {
            if (selectItem.key == keys[i]) {
                isOk = true;
            }
        }

        if (isAdmin || isOk) {
            if (treeSelected()) {
                nav(url + "Create?ParentId=" + treeSelected());
            }
            else {
                nav(url + "Create");
            }
        } else {
            alert('只能对 课程培训，作品展示，新闻资讯 进行新建');
        }
    }

    function modify() {
        if (!treeSelected()) return alert("未选中任何节点！");
        nav(url + "Modify?Id=" + treeSelected());
    }

    function config() {
        if (!treeSelected()) return alert("未选中任何节点！");
        nav(url + "Config?Id=" + treeSelected());
    }

    function applyConfig() {
        if (!treeSelected()) return alert("未选中任何节点！");
        nav(url + "ApplyConfig?Id=" + treeSelected());
    }

    function removee() {
        if (!treeSelected()) return alert("未选中任何节点！");
        if (!confirm("确定要删除选中的数据吗？")) return;

        $.ajax
            ({
                url: url + "Remove",
                data: { id: treeSelected() },
                type: "post",
                error: disposeError,
                success: function (result) {
                    if (disposeException(result)) return;
                    reload(treeSelected());
                }
            });
    }

    function copy() {
        if (!treeSelected()) return alert("未选中任何节点！");

        $.ajax
            ({
                url: url + "Copy",
                data: { id: treeSelected() },
                type: "post",
                error: disposeError,
                success: function (result) {
                    if (disposeException(result)) return;
                    reload();
                }
            });
    }

    function move(sourceId, destinationId, position) {
        $.ajax
            ({
                url: url + "Move",
                data: { sourceId: sourceId, destinationId: destinationId, position: position },
                type: "post",
                error: disposeError,
                success: function (result) {
                    if (disposeException(result)) return;
                }
            });
    }

    function detail() {
        $.ajax
            ({
                url: url + "Detail",
                data: { id: treeSelected() },
                type: "post",
                error: disposeError,
                success: function (result) {
                    if (disposeException(result)) return;
                    bind(result);
                }
            });
    }

</script>
